﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Drafter.Messages;
using Drafter.Model;

namespace Drafter.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class EditViewModel : ViewModelBase
    {


        #region commands
        public RelayCommand CloseApplicationCommand
        {
            get;
            private set;
        }

        public RelayCommand OpenFileCommand
        {
            get;
            private set;
        }
        public RelayCommand SaveFileCommand
        {
            get;
            private set;
        }
        public RelayCommand SaveFileAsCommand
        {
            get;
            private set;
        }
        public RelayCommand NewFileCommand
        {
            get;
            private set;
        }
        public RelayCommand PrintCommand
        {
            get;
            private set;
        }
        public RelayCommand ExportCommand
        {
            get;
            private set;
        }
        public RelayCommand<string> NavigateToCommand
        {
            get;
            private set;
        }
        public RelayCommand ToogleOutlineView
        {
            get;
            private set;
        }
        public RelayCommand ToogleNotesView
        {
            get;
            private set;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the EditViewModel class.
        /// </summary>
        public EditViewModel()
        {
            InitializeCommands();

            if (IsInDesignMode)
            {
             

                // Code runs in Blend --> create design time data.
            }
            else
            {
                // Code runs "for real": Connect to service, etc...
            }
        }

        private void InitializeCommands()
        {
            CloseApplicationCommand = new RelayCommand(() =>
            {
                System.Windows.Application.Current.Shutdown();
            });

            OpenFileCommand = new RelayCommand(() =>
            {
                Messenger.Default.Send<OpenFileDialogMessage>(new OpenFileDialogMessage("Open My File", OpenFile, ".draft", "Drafts|*.draft"));
            });

            SaveFileCommand = new RelayCommand(() =>
            {
                Messenger.Default.Send<SaveFileDialogMessage>(new SaveFileDialogMessage("Save My File", SaveFile, ".draft", "Drafts|*.draft"));
            });
            SaveFileAsCommand = new RelayCommand(() =>
            {
                Messenger.Default.Send<SaveFileDialogMessage>(new SaveFileDialogMessage("Save My File", SaveFile, ".draft", "Drafts|*.draft"));
            });
            PrintCommand = new RelayCommand(() =>
            {
              Messenger.Default.Send<PrintDialogMessage>(new PrintDialogMessage(_draftViewModel.Title, _draftViewModel.GetPaginator()));
            });
            ExportCommand = new RelayCommand(() =>
            {
                Messenger.Default.Send<SaveFileDialogMessage>(new SaveFileDialogMessage("Export My File", ExportFile, ".txt", "Text Files|*.txt"));
            });
            NavigateToCommand = new RelayCommand<string>((target) =>
            {
                Messenger.Default.Send<string>(target);
            });

            ToogleNotesView = new RelayCommand(() =>
            {
                NotesVisible = !NotesVisible;
                
            });
            ToogleOutlineView = new RelayCommand(() =>
            {
                OutlineVisible = !OutlineVisible;
            });
        }

        public void OpenFile(string filename)
        {
            _draftViewModel.Load(filename);
        }

        public void SaveFile(string filename)
        {
            _draftViewModel.Save(filename);
        }

        public void ExportFile(string filename)
        {
            _draftViewModel.Export(filename);
        }


        
        ////public override void Cleanup()
        ////{
        ////    // Clean own resources if needed

        ////    base.Cleanup();
        ////}
        /// <summary>
        /// The <see cref="DraftViewModel" /> property's name.
        /// </summary>
        public const string DraftViewModelPropertyName = "DraftViewModel";

        private DraftViewModel _draftViewModel = new DraftViewModel();



        /// <summary>
        /// The <see cref="OutlineVisible" /> property's name.
        /// </summary>
        public const string OutlineVisiblePropertyName = "OutlineVisible";

        private bool _outlineVisible = true;

        /// <summary>
        /// Gets the OutlineVisible property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public bool OutlineVisible
        {
            get
            {
                return _outlineVisible;
            }

            set
            {
                if (_outlineVisible == value)
                {
                    return;
                }

                var oldValue = _outlineVisible;
                _outlineVisible = value;


                // Update bindings, no broadcast
                RaisePropertyChanged(OutlineVisiblePropertyName);

         
            }
        }

        /// <summary>
        /// The <see cref="NotesVisible" /> property's name.
        /// </summary>
        public const string NotesVisiblePropertyName = "NotesVisible";

        private bool _notesVisible = true;

        /// <summary>
        /// Gets the NotesVisible property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public bool NotesVisible
        {
            get
            {
                return _notesVisible;
            }

            set
            {
                if (_notesVisible == value)
                {
                    return;
                }

                var oldValue = _notesVisible;
                _notesVisible = value;



                // Update bindings, no broadcast
                RaisePropertyChanged(NotesVisiblePropertyName);

     
            }
        }

        /// <summary>
        /// Gets the DraftViewModel property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public DraftViewModel DraftViewModel
        {
            get
            {
                return _draftViewModel;
            }

            set
            {
                if (_draftViewModel == value)
                {
                    return;
                }

                var oldValue = _draftViewModel;
                _draftViewModel = value;


                // Update bindings, no broadcast
                RaisePropertyChanged(DraftViewModelPropertyName);


            }
        }
    }
}